//
//  LGTViewCreateUtil.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/11.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTViewCreateUtil: NSObject {
    static let share = LGTViewCreateUtil()
    
    func createImageViewCorner(corner:CGFloat = 0, imageName:String = "", model:UIView.ContentMode = UIView.ContentMode.scaleAspectFill) ->UIImageView{
        let imageView:UIImageView = UIImageView.init()
        if corner > 0{
            imageView.layer.masksToBounds = true
            imageView.layer.cornerRadius = corner
        }
        
        if imageName != ""{
            imageView.image = UIImage.init(named: imageName)
        }
        
        //imageView.backgroundColor = UIColor.orange
        
        return imageView
    }
    
    func createLabel(textAlignment:NSTextAlignment, textColor:UIColor, fontSize:CGFloat, text:String = "", corner:CGFloat = 0) -> UILabel{
        let titleLabel:UILabel = UILabel.init()
        titleLabel.textAlignment = textAlignment
        titleLabel.text =  text
        titleLabel.textColor = textColor
        titleLabel.font = BaseUtil.baseUtil.getFont(size: fontSize)
        titleLabel.sizeToFit()
        //titleLabel.adjustsFontSizeToFitWidth = true
        if corner > 0{
            titleLabel.layer.masksToBounds = true
            titleLabel.layer.cornerRadius = corner
        }
        
        return titleLabel
    }
    
    func createButtonTitleAndImage(imageName:String, title:String, color:UIColor, font:UIFont, backColor:UIColor, left:CGFloat=SizeFitFloat(10)) ->UIButton{
        let button:UIButton = UIButton.init()
        button.setImage(UIImage.init(named: imageName), for: UIControl.State.normal)
        button.setImage(UIImage.init(named: imageName), for: UIControl.State.selected)
        button.setTitle(title, for: UIControl.State.normal)
        button.setTitle(title, for: UIControl.State.selected)
        button.setTitleColor(color, for: UIControl.State.normal)
        button.setTitleColor(color, for: UIControl.State.selected)
        button.titleLabel?.font = font
        button.backgroundColor = backColor
        button.imageView?.contentMode = UIView.ContentMode.scaleAspectFill
        let x0:CGFloat = CGFloat(0)
        button.titleEdgeInsets = UIEdgeInsets.init(top: x0, left: left, bottom: x0, right: x0)
        //button.imageEdgeInsets = UIEdgeInsets.init(top: x0, left: SizeFitFloat(5), bottom: x0, right: x0)
        return button
    }
    
    func createButton(image:String, selected:String) -> UIButton{
        let btn:UIButton = UIButton.init();
        btn.setBackgroundImage(UIImage.init(named: image), for: UIControl.State.normal)
        btn.setBackgroundImage(UIImage.init(named: image), for: UIControl.State.selected)
        btn.isHighlighted = false
        return btn
    }
    
    func createLGTButton(image:String, selected:String, dW:CGFloat, dH:CGFloat) -> LGTButton{
        let btn:LGTButton = LGTButton.init();
        btn.setBackgroundImage(UIImage.init(named: image), for: UIControl.State.normal)
        btn.setBackgroundImage(UIImage.init(named: selected), for: UIControl.State.selected)
        btn.initPointAction(dw: dW, dh: dH)
        btn.isHighlighted = false
        return btn
    }
    
    func createTextLGTButton(title:String, font:UIFont, color:UIColor, selectedColor:UIColor, dW:CGFloat = 0, dH:CGFloat = 0, corner:CGFloat = 0, backColor:UIColor = UIColor.white, borderWidth:CGFloat=0, borderColor:UIColor=UIColor.clear) -> LGTButton{
        let btn:LGTButton = LGTButton.init()
        btn.setTitle(title, for: UIControl.State.normal)
        btn.setTitle(title, for: UIControl.State.selected)
        btn.setTitleColor(color, for: UIControl.State.normal)
        btn.setTitleColor(selectedColor, for: UIControl.State.selected)
        btn.initPointAction(dw: dW, dh: dH)
        btn.titleLabel?.font = font
        btn.isHighlighted = false
        btn.backgroundColor = backColor
        
        if corner > 0{
            btn.layer.masksToBounds = true
            btn.layer.cornerRadius = corner
        }
        
        if borderWidth > 0{
            btn.layer.borderWidth = borderWidth
            btn.layer.borderColor = borderColor.cgColor
        }
        
        return btn
    }
    
    func createTaskTypeSelectButton(position: LGTTaskTypeSelectButton.Position, spaceX:CGFloat = 0, title:String, image:String, selectImage:String, font:UIFont, color:UIColor, selectedColor:UIColor, dW:CGFloat = 0, dH:CGFloat = 0) -> LGTTaskTypeSelectButton{
        let btn:LGTTaskTypeSelectButton = LGTTaskTypeSelectButton.init(position, space: spaceX)
        btn.setTitle(title, for: UIControl.State.normal)
        btn.setTitle(title, for: UIControl.State.selected)
        btn.setTitleColor(color, for: UIControl.State.normal)
        btn.setTitleColor(selectedColor, for: UIControl.State.selected)
        btn.initPointAction(dw: dW, dh: dH)
        btn.titleLabel?.font = font
        btn.isHighlighted = false
        btn.setImage(UIImage.init(named: image), for: UIControl.State.normal)
        btn.setImage(UIImage.init(named: selectImage), for: UIControl.State.selected)
        return btn
    }
    
    //LGTTaskTypeSelectView
    
    func createImageView(image:String, contentMode:UIView.ContentMode = UIView.ContentMode.scaleAspectFill) -> UIImageView{
        let imageView:UIImageView = UIImageView.init()
        imageView.image = UIImage.init(named: image)
        imageView.contentMode = UIView.ContentMode.scaleAspectFill
        return imageView
    }
    
    func createTextFied(font:UIFont, keyBoardType:UIKeyboardType = UIKeyboardType.`default`, placeholder:String="", textColor:UIColor)->UITextField{
        let textField:UITextField  = UITextField.init()
        textField.font = font
        textField.placeholder = placeholder
        textField.textColor = textColor
        textField.keyboardType = keyBoardType
        return textField
    }
    
    
    func createLGTTextFied(font:UIFont, keyBoardType:UIKeyboardType = UIKeyboardType.`default`, placeholder:String="", textColor:UIColor, limitCout:Int = -1)-> LGTTextField{
        let textField:LGTTextField  = LGTTextField.init()
        textField.font = font
        textField.placeholder = placeholder
        textField.textColor = textColor
        textField.keyboardType = keyBoardType
        textField.limitCount = limitCout
        return textField
    }
    
    func createAgreenText() -> UITextView{
        
        let textView:UITextView = self.createSpecialTextView(agreenStr: "登录即代表阅读并同意《用户服务协议》", specialStr: "《用户服务协议》", normalColor: GrayColor, speailColor: BlueColor, font: Font_Normal_12, textAlignment: NSTextAlignment.center)
        
//        let textView:UITextView = LGTTextView.init()
//        textView.isEditable = false
//        textView.textAlignment = NSTextAlignment.center
//        textView.allowsEditingTextAttributes = true
//        //textView.isSelectable = false
//        textView.isScrollEnabled = false
//        textView.backgroundColor = UIColor.clear
//        textView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
//        textView.textColor = GrayColor
////        textView.alwaysBounceVertical = false
////        textView.alwaysBounceHorizontal = false
//
//        //链接颜色设置
//        textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor:BlueColor]
//        let agreenStr:String = "登录即代表阅读并同意《用户服务协议》"
//        let specialStr:String = "《用户服务协议》"
//        let attr:NSMutableAttributedString = NSMutableAttributedString.init(string: agreenStr)
//        attr.addAttributes([NSAttributedString.Key.foregroundColor:GrayColor,
//                            NSAttributedString.Key.font:Font_Normal_12], range: NSRange.init(location: 0, length: agreenStr.count))
//        let range = agreenStr.range(of: specialStr)!
//        let nsrange = NSRange(range, in: agreenStr) // Range to NSRange
//        attr.addAttributes([NSAttributedString.Key.foregroundColor:BlueColor,
//                            NSAttributedString.Key.link:"http://baidu.com",
//                            NSAttributedString.Key.font:Font_Normal_12
//            ], range: nsrange)
//
//        textView.attributedText = attr
//        if #available(iOS 11.0, *){
//            textView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
//        }
        return textView
    }
    
    func createSpecialTextView(agreenStr:String, specialStr:String, normalColor:UIColor, speailColor:UIColor, font:UIFont, textAlignment:NSTextAlignment) -> UITextView{
        let textView:UITextView = LGTTextView.init()
        textView.isEditable = false
        textView.textAlignment = textAlignment
        textView.allowsEditingTextAttributes = true
        //textView.isSelectable = false
        textView.isScrollEnabled = false
        textView.backgroundColor = UIColor.clear
        textView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        textView.textColor = GrayColor
        
        //链接颜色设置
        textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor:speailColor]
        let attr:NSMutableAttributedString = NSMutableAttributedString.init(string: agreenStr)
        attr.addAttributes([NSAttributedString.Key.foregroundColor:normalColor,
                            NSAttributedString.Key.font:font], range: NSRange.init(location: 0, length: agreenStr.count))
        let range = agreenStr.range(of: specialStr)!
        let nsrange = NSRange(range, in: agreenStr) // Range to NSRange
        attr.addAttributes([NSAttributedString.Key.foregroundColor:speailColor,
                            NSAttributedString.Key.link:"http://baidu.com",
                            NSAttributedString.Key.font:Font_Normal_12
            ], range: nsrange)
        
        textView.attributedText = attr
        if #available(iOS 11.0, *){
            textView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
        }
        return textView
    }
    
    
    func createPublicAgreenText() -> UITextView{
        let textView:UITextView = LGTTextView.init()
        textView.isEditable = false
        textView.textAlignment = NSTextAlignment.center
        textView.allowsEditingTextAttributes = true
        textView.isScrollEnabled = false
        textView.backgroundColor = UIColor.clear
        textView.textContainerInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        textView.textColor = TextColor
       // textView.textContainer = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        
        //链接颜色设置
        textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor:YellowColor]
        let agreenStr:String = "我已阅读并遵守《发布任务规则》"
        let specialStr:String = "《发布任务规则》"
        let attr:NSMutableAttributedString = NSMutableAttributedString.init(string: agreenStr)
        attr.addAttributes([NSAttributedString.Key.foregroundColor:TextColor,
                            NSAttributedString.Key.font:Font_Normal_15], range: NSRange.init(location: 0, length: agreenStr.count))
        let range = agreenStr.range(of: specialStr)!
        let nsrange = NSRange(range, in: agreenStr) // Range to NSRange
        attr.addAttributes([NSAttributedString.Key.foregroundColor:YellowColor,
                            NSAttributedString.Key.link:"http://baidu.com",
                            NSAttributedString.Key.font:Font_Normal_15
            ], range: nsrange)
        
        textView.attributedText = attr
        if #available(iOS 11.0, *){
            textView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
        }
        
        return textView
    }
    
    
    func createLineView() -> UIImageView{
        let image:UIImageView = UIImageView.init()
        image.backgroundColor = AllLineColor
        return image
    }
    
    func createInputTextView(placehoder:String, fontSize:CGFloat, limitCout:Int=0, showCount:Bool = false) -> LGTInputTextView{
        let textView:LGTInputTextView = LGTInputTextView.init(frame: CGRect.zero)
        textView.updateInfo(fontSize: fontSize, textColor: TextColor, limitCount: limitCout, placeHoder: placehoder)
        textView.showCount = showCount
        return textView
    }
}
